﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace UpdateAssistant
{
    public class ColoredProgressBar : ProgressBar
    {
        public Color ProgressColor { get; set; } = Color.Green; // 默认填充颜色

        public ColoredProgressBar()
        {
            this.SetStyle(ControlStyles.UserPaint, true); // 启用自定义绘制
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle rect = this.ClientRectangle;
            using (Graphics g = e.Graphics)
            {
                // 绘制进度条背景
                g.FillRectangle(new SolidBrush(SystemColors.Control), rect);

                // 计算填充区域
                double fillPercent = ((double)this.Value - this.Minimum) / (this.Maximum - this.Minimum);
                int fillWidth = (int)(rect.Width * fillPercent);
                Rectangle fillRect = new Rectangle(0, 0, fillWidth, rect.Height);

                // 绘制填充颜色
                if (fillRect.Width > 0)
                {
                    g.FillRectangle(new SolidBrush(this.ProgressColor), fillRect);
                }

                // 绘制边框（可选）
                g.DrawRectangle(Pens.Gray, 0, 0, rect.Width - 1, rect.Height - 1);
            }
        }
    }
}
